<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 登录方式选择 -->
      <div class="login-type">
        <div 
          class="type-item" 
          :class="{ active: loginType === 'account' }"
          @click="loginType = 'account'"
        >账号登录</div>
        <div 
          class="type-item" 
          :class="{ active: loginType === 'sms' }"
          @click="loginType = 'sms'"
        >短信登录</div>
      </div>
      
      <!-- 登录表单 -->
      <div class="login-form">
        <!-- 账号登录表单 -->
        <template v-if="loginType === 'account'">
          <!-- 账号输入 -->
          <div class="input-item">
            <el-input 
              v-model="loginForm.username" 
              placeholder="请输入账号"
              :prefix-icon="User"
              size="large"
            />
          </div>
          
          <!-- 密码输入 -->
          <div class="input-item">
            <el-input 
              v-model="loginForm.password" 
              type="password" 
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
              size="large"
            />
          </div>
          
          <!-- 验证码 -->
          <div class="input-item verification">
            <el-input 
              v-model="verificationCode" 
              placeholder="验证码"
              :prefix-icon="Key"
              size="large"
            />
            <div id="v_container"></div>
          </div>
          
          <!-- 登录按钮 -->
          <div class="buttons">
            <button class="login-btn" @click="handleLogin">登录</button>
          </div>
          
          <!-- 底部链接 -->
          <div class="links">
            <a href="#" class="forgot-pwd">忘记密码?</a>
            <span class="register-tip">
              还未注册？<router-link to="/register" class="register-link">立即注册</router-link>
            </span>
          </div>
        </template>

        <!-- 短信登录表单 -->
        <template v-if="loginType === 'sms'">
          <!-- 手机号输入 -->
          <div class="input-item">
            <el-input 
              v-model="smsForm.phone" 
              placeholder="请输入手机号"
              :prefix-icon="Iphone"
              size="large"
            />
          </div>
          
          <!-- 短信验证码 -->
          <div class="input-item verification">
            <el-input 
              v-model="smsForm.code" 
              placeholder="请输入验证码"
              :prefix-icon="Message"
              size="large"
            />
            <el-button 
              type="primary" 
              :disabled="!!timer"
              @click="sendSmsCode"
            >{{ timer ? `${countdown}s后重新获取` : '获取验证码' }}</el-button>
          </div>
          
          <!-- 登录按钮 -->
          <div class="buttons">
            <button class="login-btn" @click="handleSmsLogin">登录</button>
          </div>
          

        </template>
      </div>

      <!-- 其他登录方式 -->
      <div class="other-login">
        <div class="divider">
          <span>使用其他方式登录</span>
        </div>
        <div class="social-login">
          <img src="@/assets/wechat.png" alt="微信登录">
        </div>
      </div>
    </div>
    
    <!-- 背景图 -->
    <div class="background">
      <img src="@/assets/background.png" alt="背景">
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { userApi } from '@/api'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Key, Iphone, Message } from '@element-plus/icons-vue'
import { GVerify } from '@/utils/verifyCode'

const router = useRouter()
const loginType = ref('account') // 登录方式：account 或 sms
const verificationCode = ref('')
const verifyCode = ref(null)
const smsForm = ref({
  phone: '',
  code: ''
})
const countdown = ref(60)
const timer = ref(null)

const loginForm = ref({
  username: '',
  password: ''
})

onMounted(() => {
  // 初始化验证码，使用混合类型
  verifyCode.value = new GVerify({
    id: 'v_container',
    type: 'blend', // blend:数字字母混合类型、number:纯数字、letter:纯字母
    width: 140,
    height: 40
  })
})

const handleLogin = async () => {
  try {
    // 表单验证
    if (!loginForm.value.username || !loginForm.value.password) {
      ElMessage.warning('请输入用户名和密码')
      return
    }

    // 验证码验证
    if (!verificationCode.value) {
      ElMessage.warning('请输入验证码')
      return
    }

    // 验证验证码
    if (!verifyCode.value.validate(verificationCode.value)) {
      ElMessage.error('验证码错误')
      verificationCode.value = ''
      return
    }

    const res = await userApi.login(loginForm.value)
    if (res.code === 200) {
      ElMessage.success('登录成功')
      localStorage.setItem('token', res.data.token)
      router.push('/task')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败')
  }
}

// 发送短信验证码
const sendSmsCode = async () => {
  if (!smsForm.value.phone) {
    ElMessage.warning('请输入手机号')
    return
  }
  
  // 验证图形验证码
  if (!verificationCode.value) {
    ElMessage.warning('请输入图形验证码')
    return
  }
  
  if (!verifyCode.value.validate(verificationCode.value)) {
    ElMessage.error('图形验证码错误')
    verificationCode.value = ''
    return
  }

  try {
    // 调用发送短信验证码接口
    await userApi.sendSmsCode(smsForm.value.phone)
    ElMessage.success('验证码发送成功')
    
    // 开始倒计时
    countdown.value = 60
    timer.value = setInterval(() => {
      countdown.value--
      if (countdown.value <= 0) {
        clearInterval(timer.value)
        timer.value = null
      }
    }, 1000)
  } catch (error) {
    console.error('发送验证码失败:', error)
    ElMessage.error(error.message || '发送验证码失败')
  }
}

// 短信登录
const handleSmsLogin = async () => {
  try {
    if (!smsForm.value.phone || !smsForm.value.code) {
      ElMessage.warning('请输入手机号和验证码')
      return
    }

    const res = await userApi.smsLogin(smsForm.value)
    if (res.code === 200) {
      ElMessage.success('登录成功')
      localStorage.setItem('token', res.data.token)
      router.push('/task')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败')
  }
}

// 组件卸载时清除定时器
onUnmounted(() => {
  if (timer.value) {
    clearInterval(timer.value)
  }
})
</script>

<style scoped>
/* 重置一些基础样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* 确保根元素和body占满整个视口 */
:root, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
}

.login-container {
  display: flex;
  height: 100vh;
  width: 100vw;
  justify-content: center;
  align-items: center;
  position: relative;
}

.login-box {
  width: 360px;
  background: #fff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
}

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo h2 {
  font-size: 20px;
  color: #333;
  font-weight: normal;
}

.input-item {
  margin-bottom: 20px;
}

.verification {
  display: flex;
  gap: 10px;
}

.verification :deep(.el-input) {
  flex: 1;
}

.buttons {
  margin: 24px 0 16px;
}

.login-btn {
  width: 100%;
  height: 40px;
  background: #4169e1;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  margin-top: 5px;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background: #3157d5;
}

.links {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
}

.links a {
  color: #666;
  text-decoration: none;
  transition: color 0.3s;
}

.links a:hover {
  color: #333;
  background-color: transparent;
}

.register-tip {
  color: #666;
}

/* 修改选择器优先级 */
.links .register-link,
.register-link {
  color: #1890ff;
  text-decoration: none;
  transition: color 0.3s;
}

.links .register-link:hover,
.register-link:hover {
  color: #096dd9;
  background-color: transparent;
}

/* 输入框样式调整 */
:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1890ff inset;
}

/* 验证码容器样式 */
#v_container {
  width: 120px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
}

/* 图标颜色 */
:deep(.el-input__prefix-icon),
:deep(.el-input__suffix-icon) {
  color: #1890ff;
}

/* 登录方式选择 */
.login-type {
  display: flex;
  justify-content: center;
  gap: 60px;
  margin-bottom: 35px;
  position: relative;
}

.login-type::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 0;
  width: 100%;
  height: 1px;
  background: #f0f0f0;
}

.type-item {
  font-size: 14px;
  color: #666;
  cursor: pointer;
  padding: 0 8px 10px;
  position: relative;
}

.type-item.active {
  color: #1890ff;
}

.type-item.active::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 0;
  width: 100%;
  height: 2px;
  background: #1890ff;
  z-index: 1;
}

/* 其他登录方式 */
.other-login {
  margin-top: 30px;
  text-align: center;
}

.divider {
  position: relative;
  margin-bottom: 20px;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: 1px;
  background: #f0f0f0;
}

.divider span {
  position: relative;
  background: #fff;
  padding: 0 12px;
  color: #999;
  font-size: 12px;
}

.social-login img {
  width: 32px;
  cursor: pointer;
}

/* 背景图 */
.background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  background-color: #f5f7fa;
}

.background img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.6;
}

/* 移除之前的样式 */
.background-section {
  display: none;
}

/* 获取验证码按钮样式 */
.verification .el-button {
  width: 120px;
  height: 40px;
  padding: 0;
  font-size: 14px;
}

/* 移除所有链接的默认焦点和点击效果 */
.login-box a:focus,
.login-box a:focus-visible,
.login-box a:active {
  outline: none;
  background-color: transparent;
  box-shadow: none;
}
</style> 